<?php

$arr = array(2, 5, 3, 2, 3, 0, 8, 1);

function findMaxInHeap($arr, $size) {  
	for ($j = $size - 1; $j > 0; --$j) {  
		$parent = $j / 2;  
		$child = $j;  
		if ($j < $size - 1 && $arr[$j] < $arr[$j+1]) {  
			++$child;  
		}  
		if ($arr[$child] > $arr[$parent]) {  
			$tmp = $arr[$child];  
			$arr[$child] = $arr[$parent];  
			$arr[$parent] = $tmp;  
		}
	}  
}

function heapSort($arr, $size) {
	for ($j = $size; $j > 0; --$j) {
		findMaxInHeap($arr, $j);
		$tmp = $arr[0];  
		$arr[0] = $arr[$j - 1];  
		$arr[$j - 1] = $tmp;  
	}
	return $arr;
}

$n = count($arr) / count($arr[0]);  
var_dump(heapSort($arr, $n));
